<?php

namespace backend\models;

use Yii;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\behaviors\TimestampBehavior;
use yii\db\Exception;
/**
 * This is the model class for table "Project".
 *
 */
class Project extends ActiveRecord
{

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%project}}';
    }



    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['project_name','pic','character1','character1_info','character2','character2_info','character3','character3_info','character4','character4_info'], 'trim'],
            [['id','project_name','pic','character1','character1_info','character2','character2_info','character3','character3_info','character4','character4_info'], 'required', 'message'=>'{attribute}不能为空'],
            [['id'], 'match', 'pattern' => '/^[1-9][0-9]*$/','message'=>'{attribute}值无效'],
//            [['project_name'], 'unique', 'message'=>'{attribute}已存在','on' => ['create']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id'=>'项目id',
            'project_name'=>'项目名称',
            'pic'=>'项目图片',
            'character1' => '项目特性一',
            'character1_info' => '项目特性一介绍',
            'character2' => '项目特性二',
            'character2_info' => '项目特性二介绍',
            'character3' => '项目特性三',
            'character3_info' => '项目特性三介绍',
            'character4' => '项目特性四',
            'character4_info' => '项目特性四介绍',
        ];
    }



    public function scenarios()
    {
        return [
            'default' => [
                'id',
                'project_name',
                'pic',
                'character1',
                'character1_info',
                'character2',
                'character2_info',
                'character3',
                'character3_info',
                'character4',
                'character4_info',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
            'create' => [
                'project_name',
                'pic',
                'character1',
                'character1_info',
                'character2',
                'character2_info',
                'character3',
                'character3_info',
                'character4',
                'character4_info',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
            'update_status' => [
                'id',
            ],
            'update' => [
                'id',
                'project_name',
                'pic',
                'character1',
                'character1_info',
                'character2',
                'character2_info',
                'character3',
                'character3_info',
                'character4',
                'character4_info',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
        ];
    }



    /**
     * 获取项目列表
     * @param $params
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function getProjectList($params){
        if(!empty($params['project_name'])){
            $condition= ['and',['like','project_name',trim($params['project_name'])],['deleted_at'=>null,'status'=>1]];
        }else{
            $condition = ['deleted_at'=>null,'status'=>1];
        }
        $list = (new Query())
            ->select('id,project_name,status,pic,character1,character1_info,character2,character2_info,character3,character3_info,character4,character4_info,FROM_UNIXTIME(created_at) as create_time')
            ->from('project')
            ->where($condition)
            ->all();
        return empty($list)?array():$list;

    }

    /**
     * 获取项目详情
     * @param $params
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function getProjectDetail($params){
        $condition = ['deleted_at'=>null,'status'=>1,'id'=>intval($params['id'])];
        $list = (new Query())
            ->select('id,project_name,status,pic,character1,character1_info,character2,character2_info,character3,character3_info,character4,character4_info,FROM_UNIXTIME(created_at) as create_time')
            ->from('project')
            ->where($condition)
            ->one();
        return empty($list)?array():$list;

    }



    /**
     * 新增项目
     * @param $param
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function addProjectInfo($param){
        $t = static::getDb()->beginTransaction();
        try {
            $model = new self();
            $model->scenario = 'create';
            unset($param['id']);
            unset($param['status']);
            $model->setAttributes($param, false);
            if (!$model->save()) {
                $t->rollBack();
                return array('error'=>'操作失败');
            }else{
                $model->refresh();
                $t->commit();
                return array('error'=>'');
            }
        }catch (Exception $e) {
            $t->rollback();
            return array('error'=>'操作失败');
        }

    }


    /**
     * 编辑项目
     * @param $param
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function editProjectInfo($param){
        $t = static::getDb()->beginTransaction();
        try {
            $model = static::findOne(['id' =>$param['id']]);
            if(empty($model)) return array('error'=>'未查询到相关信息');
            if(!empty($param['project_name'])) {
                $have_projectname = $this->find()->where(['project_name' => $param['project_name'],'deleted_at'=>null])->andWhere(['<>', 'id', intval($param['id'])])->asArray()->one();
                if (!empty($have_projectname)) return array('error' => '项目名称已存在');
            }
            unset($param['id']);
            unset($param['status']);
            $model->setAttributes($param, false);
            if (!$model->save()) {
                $t->rollBack();
                return array('error'=>'操作失败');
            }else{
                $model->refresh();
                $t->commit();
                return array('error'=>'');
            }
        }catch (Exception $e) {
            $t->rollback();
            return array('error'=>'操作失败');
        }

    }


    /**
     * 删除项目
     * @param $param
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function deleteProjectInfo($param){
        $t = static::getDb()->beginTransaction();
        try {
            $model = static::findOne(['id' =>$param['id']]);
            if(empty($model)) return array('error'=>'未查询到相关信息');
            if($model->status == 2) return array('error'=>'');
            unset($param['id']);
            $data['status']=2;
            $data['deleted_at'] = time();
            $model->setAttributes($data, false);
            if (!$model->save()) {
                $t->rollBack();
                return array('error'=>'操作失败');
            }else{
                $model->refresh();
                $t->commit();
                return array('error'=>'');
            }
        }catch (Exception $e) {
            $t->rollback();
            return array('error'=>'操作失败');
        }

    }



    /**
     * 批量删除项目
     * @param $param
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function batchDelProjectInfo($param){
        $t = static::getDb()->beginTransaction();
        try {
            $ids=explode(',',$param['ids']);
            if(empty($ids)) return array('error'=>'参数错误');
            $res=$this::updateAll(['status' => 2,'deleted_at'=>time()], ['in','id' ,$ids]);
            if (!$res) {
                $t->rollBack();
                return array('error'=>'操作失败');
            }else{
                $this->refresh();
                $t->commit();
                return array('error'=>'');
            }
        }catch (Exception $e) {
            $t->rollback();
            return array('error'=>'操作失败');
        }

    }



}
